package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * The type Student service.
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean<Student> selectStudentByPage(Integer page, Integer pageSize, String name, Integer highestDegree, Integer classesId, String studentNumber) {
        PageBean<Student> studentPageBean = new PageBean<>();
        // 开启分页
        PageHelper.startPage(page, pageSize);
        List<Student> studentList = studentMapper.selectStudentByPage(name, highestDegree, classesId, studentNumber);
        // 类型转换
        Page<Student> studentPage = (Page<Student>) studentList;
        long total = studentPage.getTotal();
        List<Student> result = studentPage.getResult();
        studentPageBean.setTotal(total);
        studentPageBean.setRows(result);
        return studentPageBean;
    }

    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        if (student.getName().length() > 10 || student.getName().length() < 2) {
            throw new RuntimeException("学员姓名非法");
        }
        String studentNumber = student.getStudentNumber();
        if (!studentNumber.matches("^[a-zA-Z0-9]{10}$")) {
            throw new RuntimeException("学号只能是数字或字母的十位表达");
        }
        // 通过学号查询学号是否重复
        Integer count = studentMapper.selectCountByStudentNumber(student.getStudentNumber());
        if (count > 0) {
            throw new RuntimeException("学号重复");
        }
        // 通过手机号查询手机号是否重复
        Integer countPhone = studentMapper.selectCountByPhone(student.getPhone());
        if (countPhone > 0) {
            throw new RuntimeException("手机号重复");
        }
        studentMapper.addStudent(student);
    }

    @Override
    public void deleteStudentById(Integer id) {
        studentMapper.deleteStudentById(id);
    }

    @Override
    public Student selectStudentById(Integer id) {
        return studentMapper.selectStudentById(id);
    }

    @Override
    public void updateStudentById(Student student) {
        if (student.getName().length() > 10 || student.getName().length() < 2) {
            throw new RuntimeException("学员姓名非法");
        }
        String studentNumber = student.getStudentNumber();
        if (!studentNumber.matches("^[a-zA-Z0-9]{10}$")) {
            throw new RuntimeException("学号只能是数字或字母的十位表达");
        }
        // 通过学号查询学号是否重复并且排除自身的干扰
        Integer count = studentMapper.selectCountByStudentNumberNotId(student.getStudentNumber(), student.getId());
        if (count > 0) {
            throw new RuntimeException("学号已存在");
        }
        // 通过手机号查询手机号是否重复并且排除自身的干扰
        Integer countPhone = studentMapper.selectCountByPhoneNotId(student.getPhone(), student.getId());
        if (countPhone > 0) {
            throw new RuntimeException("手机号已存在");
        }
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudentById(student);
    }

    @Override
    public void updateStudentForDeductions(Student student) {
        studentMapper.updateStudentForDeductions(student);
    }
}
